//
//  XYToolBar.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/18.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYToolBar: UIView {

    var status:XYStatusesModel?{
        didSet{
            setButtonNum()
        }
    }
    
    //初始化一个装按钮的数组
    private lazy var buttonArray : [UIButton] = [UIButton]()
    
    private lazy var lineArray:[UIImageView] = [UIImageView]()
    
    //定义三个按钮来记录添加按钮
    var retweetButton:UIButton?
    var commentButton:UIButton?
    var unlikeButton:UIButton?

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        retweetButton = addButton("timeline_icon_retweet", buttonTitle: "转发")
        commentButton = addButton("timeline_icon_comment", buttonTitle: "评论")
        unlikeButton = addButton("timeline_icon_unlike", buttonTitle: "赞")
        
        addViewLine()
        addViewLine()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //添加三个按钮
    private func addButton(imageName:String,buttonTitle:String) -> UIButton{
        let button = UIButton()
        //设置按钮图片
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        //设置button里面的文字属性
//        button.setTitle(buttonTitle, forState: UIControlState.Normal)
        button.setTitleColor(ColorDefine(r: 80, g: 80, b: 80), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5)
       
        //添加到view上
        addSubview(button)
        //添加到数组中
        buttonArray.append(button)
        
        return button
    }
    
    //添加线的view
    private func addViewLine(){
        let lineView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(lineView)
        lineArray.append(lineView)
    }
    
    
    //给按钮title赋值
    private func setButtonNum(){
        if let sta = status{
           retweetButton!.setTitle(buttonTitle(sta.reposts_count, titltName: "转发"), forState: UIControlState.Normal)
           commentButton!.setTitle(buttonTitle(sta.comments_count, titltName: "评论"), forState: UIControlState.Normal)
            unlikeButton!.setTitle(buttonTitle(sta.attitudes_count, titltName: "赞"), forState: UIControlState.Normal)
        }
    }
    
    //判断按钮中返回的字符串
    private func  buttonTitle(titleNum:Int,titltName:String) -> String{
        if titleNum != 0{
            if titleNum > 10000{
               let num = titleNum / 1000
                let  numStr = "\(CGFloat(num) / CGFloat(10))条"
              return  numStr.stringByReplacingOccurrencesOfString(".0", withString: " ")
            }
            
            return " \(titleNum)"
        }else{
            return titltName
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //按钮的宽度
        let buttonWidth = SCREENW / CGFloat(buttonArray.count)
        
        for i in 0..<buttonArray.count {
            let childButton = buttonArray[i]
            childButton.size = CGSizeMake(buttonWidth, height)
            childButton.x = buttonWidth * CGFloat(i)
            
        }
        
        for i in 0..<lineArray.count {
            let childView = lineArray[i]
            childView.centerX = CGFloat(i + 1) * buttonWidth
        }
        
    }

}











